package com.navinfo.platform.trip.common.arithmetic;

import com.navinfo.platform.trip.common.arithmetic.annotation.Arithmetic;
import com.navinfo.platform.trip.common.arithmetic.data.CommonData;
import com.navinfo.platform.trip.common.arithmetic.data.OuterStatisticData;
import com.navinfo.platform.trip.common.arithmetic.var.VarCommon;
import com.navinfo.platform.trip.common.arithmetic.var.VarRangeGasOpenPercent;
import com.navinfo.platform.trip.common.arithmetic.enums.RangeGasOpenPercentEnum;
import com.navinfo.platform.trip.common.arithmetic.manager.AbstractArithmetic;
import com.navinfo.platform.trip.common.util.ArithmeticUtils;

import java.util.Optional;

/**
 * 油门开合度算法.<br>
 * 依赖{@link IdleParking}的怠速状态.<br>
 * 思路是累加油门开合度，累计数量，行程结束的时候除一下<br>
 * 输出：统计<br>
 */
@Arithmetic(name = "平均油门开度")
public class RangeGasOpenPercent extends AbstractArithmetic<VarRangeGasOpenPercent> {

    @Override
    public void calculate() {
        logger.debug("进入平均油门开度算法.");
        VarRangeGasOpenPercent var = super.getObj();
        CommonData commonData = drivingData.getCommonData();

        VarCommon varGas = null;
        int accPedalPos = (int)commonData.getAcceleratorPosition();
        if (commonData.isWork()) {
            if (RangeGasOpenPercentEnum.RANGE_0.getRange().contains(accPedalPos)) {
                varGas= var.getGasOpenPercent0();
            } else if (RangeGasOpenPercentEnum.RANGE_10.getRange().contains(accPedalPos)) {
                varGas= var.getGasOpenPercent10();
            } else if (RangeGasOpenPercentEnum.RANGE_20.getRange().contains(accPedalPos)) {
                varGas= var.getGasOpenPercent20();
            } else if (RangeGasOpenPercentEnum.RANGE_30.getRange().contains(accPedalPos)) {
                varGas=  var.getGasOpenPercent30();
            } else if (RangeGasOpenPercentEnum.RANGE_40.getRange().contains(accPedalPos)) {
                varGas=  var.getGasOpenPercent40();
            } else if (RangeGasOpenPercentEnum.RANGE_50.getRange().contains(accPedalPos)) {
                varGas= var.getGasOpenPercent50();
            } else if (RangeGasOpenPercentEnum.RANGE_60.getRange().contains(accPedalPos)) {
                varGas= var.getGasOpenPercent60();
            } else if (RangeGasOpenPercentEnum.RANGE_70.getRange().contains(accPedalPos)) {
                varGas= var.getGasOpenPercent70();
            } else if (RangeGasOpenPercentEnum.RANGE_80.getRange().contains(accPedalPos)) {
                varGas= var.getGasOpenPercent80();
            } else if (RangeGasOpenPercentEnum.RANGE_90.getRange().contains(accPedalPos)) {
                varGas= var.getGasOpenPercent90();
            } else if (RangeGasOpenPercentEnum.RANGE_99.getRange().contains(accPedalPos)) {
                varGas=  var.getGasOpenPercent99();
            } else if (RangeGasOpenPercentEnum.RANGE_100.getRange().contains(accPedalPos)) {
                varGas= var.getGasOpenPercent100();
            }
            //累加相应转速区间的公共统计变量
            if(null != varGas){
                varGas.addMileage(commonData.getDffMileage());
                varGas.addFuel(commonData.getDffFuel());
                varGas.addDuration(commonData.getDffTime());
            }
        }

        var.setGasOpenPercentSum(var.getGasOpenPercentSum() + accPedalPos);
        var.setGasOpenPercentCount(var.getGasOpenPercentCount() + 1);

        logger.debug("平均油门开度算法结束.");
    }

    @Override
    public void collect(OuterStatisticData statisticDataOut) {
        VarRangeGasOpenPercent var = super.getObj();

        statisticDataOut.setGasOpenPercent0Duration(ArithmeticUtils.timeMsToSecond(var.getGasOpenPercent0().getDuration()));
        statisticDataOut.setGasOpenPercent0Mileage(ArithmeticUtils.mileageMToM(var.getGasOpenPercent0().getMileage()));
        statisticDataOut.setGasOpenPercent0Fuel(ArithmeticUtils.fuelMlToMl(var.getGasOpenPercent0().getFuel()));

        statisticDataOut.setGasOpenPercent10Duration(ArithmeticUtils.timeMsToSecond(var.getGasOpenPercent10().getDuration()));
        statisticDataOut.setGasOpenPercent10Mileage(ArithmeticUtils.mileageMToM(var.getGasOpenPercent10().getMileage()));
        statisticDataOut.setGasOpenPercent10Fuel(ArithmeticUtils.fuelMlToMl(var.getGasOpenPercent10().getFuel()));

        statisticDataOut.setGasOpenPercent20Duration(ArithmeticUtils.timeMsToSecond(var.getGasOpenPercent20().getDuration()));
        statisticDataOut.setGasOpenPercent20Mileage(ArithmeticUtils.mileageMToM(var.getGasOpenPercent20().getMileage()));
        statisticDataOut.setGasOpenPercent20Fuel(ArithmeticUtils.fuelMlToMl(var.getGasOpenPercent20().getFuel()));

        statisticDataOut.setGasOpenPercent30Duration(ArithmeticUtils.timeMsToSecond(var.getGasOpenPercent30().getDuration()));
        statisticDataOut.setGasOpenPercent30Mileage(ArithmeticUtils.mileageMToM(var.getGasOpenPercent30().getMileage()));
        statisticDataOut.setGasOpenPercent30Fuel(ArithmeticUtils.fuelMlToMl(var.getGasOpenPercent30().getFuel()));

        statisticDataOut.setGasOpenPercent40Duration(ArithmeticUtils.timeMsToSecond(var.getGasOpenPercent40().getDuration()));
        statisticDataOut.setGasOpenPercent40Mileage(ArithmeticUtils.mileageMToM(var.getGasOpenPercent40().getMileage()));
        statisticDataOut.setGasOpenPercent40Fuel(ArithmeticUtils.fuelMlToMl(var.getGasOpenPercent40().getFuel()));

        statisticDataOut.setGasOpenPercent50Duration(ArithmeticUtils.timeMsToSecond(var.getGasOpenPercent50().getDuration()));
        statisticDataOut.setGasOpenPercent50Mileage(ArithmeticUtils.mileageMToM(var.getGasOpenPercent50().getMileage()));
        statisticDataOut.setGasOpenPercent50Fuel(ArithmeticUtils.fuelMlToMl(var.getGasOpenPercent50().getFuel()));

        statisticDataOut.setGasOpenPercent60Duration(ArithmeticUtils.timeMsToSecond(var.getGasOpenPercent60().getDuration()));
        statisticDataOut.setGasOpenPercent60Mileage(ArithmeticUtils.mileageMToM(var.getGasOpenPercent60().getMileage()));
        statisticDataOut.setGasOpenPercent60Fuel(ArithmeticUtils.fuelMlToMl(var.getGasOpenPercent60().getFuel()));

        statisticDataOut.setGasOpenPercent70Duration(ArithmeticUtils.timeMsToSecond(var.getGasOpenPercent70().getDuration()));
        statisticDataOut.setGasOpenPercent70Mileage(ArithmeticUtils.mileageMToM(var.getGasOpenPercent70().getMileage()));
        statisticDataOut.setGasOpenPercent70Fuel(ArithmeticUtils.fuelMlToMl(var.getGasOpenPercent70().getFuel()));

        statisticDataOut.setGasOpenPercent80Duration(ArithmeticUtils.timeMsToSecond(var.getGasOpenPercent80().getDuration()));
        statisticDataOut.setGasOpenPercent80Mileage(ArithmeticUtils.mileageMToM(var.getGasOpenPercent80().getMileage()));
        statisticDataOut.setGasOpenPercent80Fuel(ArithmeticUtils.fuelMlToMl(var.getGasOpenPercent80().getFuel()));

        statisticDataOut.setGasOpenPercent90Duration(ArithmeticUtils.timeMsToSecond(var.getGasOpenPercent90().getDuration()));
        statisticDataOut.setGasOpenPercent90Mileage(ArithmeticUtils.mileageMToM(var.getGasOpenPercent90().getMileage()));
        statisticDataOut.setGasOpenPercent90Fuel(ArithmeticUtils.fuelMlToMl(var.getGasOpenPercent90().getFuel()));

        statisticDataOut.setGasOpenPercent99Duration(ArithmeticUtils.timeMsToSecond(var.getGasOpenPercent99().getDuration()));
        statisticDataOut.setGasOpenPercent99Mileage(ArithmeticUtils.mileageMToM(var.getGasOpenPercent99().getMileage()));
        statisticDataOut.setGasOpenPercent99Fuel(ArithmeticUtils.fuelMlToMl(var.getGasOpenPercent99().getFuel()));

        statisticDataOut.setGasOpenPercent100Duration(ArithmeticUtils.timeMsToSecond(var.getGasOpenPercent100().getDuration()));
        statisticDataOut.setGasOpenPercent100Mileage(ArithmeticUtils.mileageMToM(var.getGasOpenPercent100().getMileage()));
        statisticDataOut.setGasOpenPercent100Fuel(ArithmeticUtils.fuelMlToMl(var.getGasOpenPercent100().getFuel()));

        statisticDataOut.setGasOpenPercentSum((long)var.getGasOpenPercentSum());
        statisticDataOut.setGasOpenPercentCount(var.getGasOpenPercentCount());
    }
}
